unit ClientFmtU;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, dxLayoutControl, cxControls, dxLayoutLookAndFeels, cxContainer,
  cxEdit, cxRadioGroup, cxTextEdit, cxMemo, cxLookAndFeelPainters,IniFiles,
  StdCtrls, cxButtons, cxCheckBox, cxListBox, cxGroupBox;

type
  TConnectNode=Record
    ConName:String;
    SupportP:boolean;
    Note:String;
    Servers:String;
    Compress:boolean;
    UseXML:boolean;
    Username,Password,Proxy,ProxyPassword:String;
  end;

  TClientFmt = class(TForm)
    dxLayoutLookAndFeelList1: TdxLayoutLookAndFeelList;
    dxLayoutWebLookAndFeel1: TdxLayoutWebLookAndFeel;
    dxLayoutWebLookAndFeel2: TdxLayoutWebLookAndFeel;
    dxLayoutControl1Group_Root: TdxLayoutGroup;
    dxLayoutControl1: TdxLayoutControl;
    dxLayoutControl1Group2: TdxLayoutGroup;
    dxLayoutControl1Group3: TdxLayoutGroup;
    cxRadioGroup1: TcxRadioGroup;
    dxLayoutControl1Item1: TdxLayoutItem;
    dxLayoutControl1Group1: TdxLayoutGroup;
    cxButton1: TcxButton;
    dxLayoutControl1Item3: TdxLayoutItem;
    cxButton2: TcxButton;
    dxLayoutControl1Item4: TdxLayoutItem;
    cxButton3: TcxButton;
    dxLayoutControl1Item5: TdxLayoutItem;
    dxLayoutControl1Item6: TdxLayoutItem;
    cxTextEdit1: TcxTextEdit;
    dxLayoutControl1Item7: TdxLayoutItem;
    cxTextEdit2: TcxTextEdit;
    dxLayoutControl1Item8: TdxLayoutItem;
    cxTextEdit3: TcxTextEdit;
    dxLayoutControl1Item9: TdxLayoutItem;
    cxTextEdit4: TcxTextEdit;
    dxLayoutControl1Item10: TdxLayoutItem;
    ACompressData: TcxCheckBox;
    dxLayoutControl1Item11: TdxLayoutItem;
    dxLayoutControl1Group5: TdxLayoutGroup;
    cxMemo2: TcxMemo;
    dxLayoutControl1Item13: TdxLayoutItem;
    cxButton4: TcxButton;
    dxLayoutControl1Item14: TdxLayoutItem;
    cxButton5: TcxButton;
    dxLayoutControl1Item15: TdxLayoutItem;
    dxLayoutControl1Group8: TdxLayoutGroup;
    dxLayoutControl1Item16: TdxLayoutItem;
    cxListBox1: TcxListBox;
    dxLayoutControl1Item2: TdxLayoutItem;
    dxLayoutControl1Group4: TdxLayoutGroup;
    procedure cxRadioGroup1PropertiesChange(Sender: TObject);
    procedure ACompressDataPropertiesChange(Sender: TObject);
    procedure cxTextEdit1PropertiesChange(Sender: TObject);
    procedure cxTextEdit2PropertiesEditValueChanged(Sender: TObject);
    procedure cxTextEdit3PropertiesEditValueChanged(Sender: TObject);
    procedure cxTextEdit4PropertiesEditValueChanged(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure cxButton3Click(Sender: TObject);
  private
    { Private declarations }
    AIni:TIniFile;
    Connects:Array of TConnectNode;
    function GetDefaultCon: integer;
    procedure SetDefaultCon(const Value: integer);
  public
    { Public declarations }
    constructor Create(AOwner:TComponent;data:variant);overload;
    destructor Destroy;override;
    property DefaultCon:integer read GetDefaultCon write SetDefaultCon;
  end;

implementation

{$R *.dfm}

{ TClientFmt }

constructor TClientFmt.Create(AOwner: TComponent; data: variant);
var
  s:TStrings;
  i:integer;
  k:TCxRadioGroupItem;
begin
  s:=TStringList.Create;
  try
    AIni:=TIniFile.Create('Client.Ini');
    AIni.ReadSectionValues('CanUseConnect',s);
    setlength(Connects,s.Count);
    inherited create(AOwner);
    for i:=0 to s.Count-1 do
    begin
      k:=TCxRadioGroupItem(cxRadioGroup1.Properties.Items.Add);
      k.Caption:=s.ValueFromIndex[i];
      if AIni.SectionExists(s.ValueFromIndex[i]) then
      begin
        Connects[i].ConName:=s.ValueFromIndex[i];
        Connects[i].SupportP:=Aini.ReadBool(Connects[i].ConName,'Supportp',false);
        Connects[i].Note:=AIni.ReadString(Connects[i].ConName,'note','');
        Connects[i].Servers:=Aini.ReadString(Connects[i].ConName,'Servers','');
        Connects[i].Compress:=Aini.ReadBool(Connects[i].ConName,'Compress',false);
        Connects[i].UseXML:=Aini.ReadBool(Connects[i].ConName,'UseXml',false);
        if Connects[i].SupportP then
        begin
          Connects[i].Username:=AIni.ReadString(Connects[i].ConName,'UserName','');
          Connects[i].Password:=AIni.ReadString(Connects[i].ConName,'Password','');
          Connects[i].Proxy:=AIni.ReadString(Connects[i].ConName,'Proxy','');
          Connects[i].ProxyPassword:=AIni.ReadString(Connects[i].ConName,'ProxyPass','');
        end;
      end;
    end;
    DefaultCon:=Aini.ReadInteger('Setting','Connect',0);
  finally
    s.free;
  end;
end;

destructor TClientFmt.Destroy;
var
  i:integer;
begin
  if ModalResult=mrok then
  begin
    AIni.WriteInteger('Setting','Connect',DefaultCon);
    for i:=0 to high(Connects) do
    begin
      if AIni.SectionExists(Connects[i].ConName) then
      begin
        AIni.WriteString(Connects[i].ConName,'note',Connects[i].Note);
        Aini.WriteString(Connects[i].ConName,'Servers',Connects[i].Servers);
        Aini.WriteBool(Connects[i].ConName,'Compress',Connects[i].Compress);
        Aini.WriteBool(Connects[i].ConName,'UseXml',Connects[i].UseXML);
        if Connects[i].SupportP then
        begin
          AIni.writeString(Connects[i].ConName,'UserName',Connects[i].Username);
          AIni.WriteString(Connects[i].ConName,'Password',Connects[i].Password);
          AIni.WriteString(Connects[i].ConName,'Proxy',Connects[i].Proxy);
          AIni.ReadString(Connects[i].ConName,'ProxyPass',Connects[i].ProxyPassword);
        end;
      end;
    end;
  end;
  AIni.Free;
  inherited;
end;

function TClientFmt.GetDefaultCon: integer;
begin
  result:=CxRadioGroup1.ItemIndex;
end;

procedure TClientFmt.SetDefaultCon(const Value: integer);
begin
  if CxRadioGroup1.ItemIndex<>value then
    if CxRadioGroup1.Properties.Items.Count-1>=Value then
     CxRadioGroup1.ItemIndex:=Value;
end;

procedure TClientFmt.cxRadioGroup1PropertiesChange(Sender: TObject);
begin
  CxMemo2.Lines.Text:=Connects[DefaultCon].Note;
  cxListBox1.items.CommaText:=Connects[DefaultCon].Servers;
  ACompressData.Checked:=Connects[DefaultCon].Compress;
  dxLayoutControl1Group3.Enabled:=Connects[DefaultCon].SupportP;
  if Connects[DefaultCon].SupportP then
  begin
    CxTextEdit1.Text:=Connects[DefaultCon].Username;
    CxTextEdit2.Text:=Connects[DefaultCon].Password;
    CxTextEdit3.Text:=Connects[DefaultCon].Proxy;
    CxTextEdit4.Text:=Connects[DefaultCon].ProxyPassword;
  end else
  begin
    CxTextEdit1.Text:='';
    CxTextEdit2.Text:='';
    CxTextEdit3.Text:='';
    CxTextEdit4.Text:='';
  end;
end;

procedure TClientFmt.ACompressDataPropertiesChange(Sender: TObject);
begin
  Connects[DefaultCon].Compress:=ACompressData.Checked;
end;

procedure TClientFmt.cxTextEdit1PropertiesChange(Sender: TObject);
begin
  Connects[DefaultCon].Username:=CxTextEdit1.Text;
end;

procedure TClientFmt.cxTextEdit2PropertiesEditValueChanged(
  Sender: TObject);
begin
  Connects[DefaultCon].Password:=CxTextEdit2.Text;
  
end;

procedure TClientFmt.cxTextEdit3PropertiesEditValueChanged(
  Sender: TObject);
begin
  Connects[DefaultCon].Proxy:=CxTextEdit3.Text;
end;

procedure TClientFmt.cxTextEdit4PropertiesEditValueChanged(
  Sender: TObject);
begin
  Connects[DefaultCon].ProxyPassword:=CxTextEdit4.Text;
end;

procedure TClientFmt.cxButton1Click(Sender: TObject);
var
  s:string;
begin
  if InputQuery('Insert Server','Please type a server name:',s) then
  begin
    cxListBox1.Items.Add(s);
    Connects[DefaultCon].Servers:=cxListBox1.Items.CommaText;
  end;
end;
procedure TClientFmt.cxButton2Click(Sender: TObject);
begin
  if cxListBox1.ItemIndex<>-1 then
  begin
    cxListBox1.Items.Delete(cxListBox1.ItemIndex);
    Connects[DefaultCon].Servers:=cxListBox1.Items.CommaText;
  end;
end;
procedure TClientFmt.cxButton3Click(Sender: TObject);
var
  s:string;
begin
  if cxListBox1.ItemIndex<>-1 then
  begin
    s:=cxListBox1.Items.Strings[cxListBox1.ItemIndex];
    if InputQuery('Edit Server','Please Edit the server name:',s) then
    begin
      cxListBox1.Items.Add(s);
      Connects[DefaultCon].Servers:=cxListBox1.Items.CommaText;
    end;
  end;
end;

end.
